﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using mock_ehr_lib.Model;
using mock_ehr_lib.Model.Quality;
using mock_ehr_lib.Model.Summary;
using System.Xml;

namespace measures_service
{
    public class MeasuresService : IMeasuresService
    {
        public Measure[] GetEligibleMeasuresByCCD(string ccd)
        {
            var summary = getSummary(ccd);
            var measures = MeasuresRepository.GetMeasures();
            return measures.Where(m => m.IsEligible(summary)).ToArray();
        }

        public MeasureEvaluateResult[] EvaluateMeasures(string ccd, string[] nqfNos)
        {
            var summary = getSummary(ccd);
            var allMeasures = MeasuresRepository.GetMeasures();
            var measures = allMeasures.Where(m => nqfNos.Contains(m.NqfNo));
            var results = new List<MeasureEvaluateResult>();
            foreach(var measure in measures)
            {
                var result = measure.Evaluate(summary);
                results.Add(result);
            }
            return results.ToArray();
        }

        private PatientSummary getSummary(string ccd)
        {
            var ccdXml = new XmlDocument();
            ccdXml.LoadXml(ccd);
            return PatientSummary.FromCCDXml(ccdXml);
        }

        public string[] GetMeasureCategories()
        {
            return new[] { "Structure", "Core", "Heart Conditions", "Cancer", "Women's Health", "Diabetes", "Miscellaneous" };
        }
    }
}
